package com.useeinfo.oa.modules.repair.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.func.lang.TimeUtils;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.*;
import com.useeinfo.oa.modules.repair.enums.AssignTypeEnum;
import com.useeinfo.oa.modules.repair.enums.RepairOrderStatusEnum;
import com.useeinfo.oa.modules.repair.enums.RepairOrderTypeEnum;
import com.useeinfo.oa.modules.repair.enums.RepairSourceEnum;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.*;


/**
 * @Auther: xudong.sun
 * @Date: 2018/12/3 13:29
 * @Description: 维修工单表
 */
@Entity
@Table(name = "repair_order")
public class RepairOrder extends SuperClassEntity<RepairOrder> {

    /**
     * 报修编号
     */
    private String reportCode;

    /**
     * 报修人
     */
    @ManyToOne
    @JoinColumn(name = "report_user_id")
    private SystemUser reportUser;

    /**
     * 报修地址
     */
    private String reportAddress;

    /**
     * 报修内容
     */
    private String reportContent;

    /**
     * 接单人
     */
    @ManyToOne
    @JoinColumn(name = "receive_user_id")
    private SystemUser receiveUser;
    /**
     * 接单时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date receiveDate;

    /**
     * 接线人
     */
    @ManyToOne
    @JoinColumn(name = "telephone_user_id")
    private SystemUser telephoneUser;

    /**
     * 报修专业(废除)
     */
    @ManyToOne
    @JoinColumn(name = "reprot_major")
    private DictInfo reportMajor;

    /**
     * 报修专业(设备专业)
     */
    private String majorName;

    /**
     * 报修部门
     */
    @ManyToOne
    @JoinColumn(name = "organization_id")
    private Organization organization;

    /**
     * 报修来源
     */
    @Enumerated(value = EnumType.STRING)
    @Column(name = "source")
    private RepairSourceEnum repairSourceEnum;

    /**
     * 要求到场时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date requirePresenceTime;

    /**
     * 到场时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date presenceTime;

    /**
     * 要求完成时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date requireFinishTime;

    /**
     * 维修完成时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date repairFinishTime;


    /**
     * 是否维修范围
     */
    @Column(columnDefinition = "bit default 0")
    private boolean inRoutingScope;

    @Column(columnDefinition = "bit default 0")
    private boolean transfer;

    /**
     * 故障原因
     */
    private String faultReason;

    /**
     * 采取措施
     */
    private String measure;

    /**
     * 未完成理由
     */
    private String unrepairReason;

    /**
     * 是否维修范围
     */
    @Column(columnDefinition = "bit default 1")
    private boolean inRepairScope;

    /**
     * 报修等级(数据字典中的)
     */
    @ManyToOne
    @JoinColumn(name = "report_level")
    private RepairLevel reportLevel;

    /**
     * 维修单类型(数据字典中的)
     */
    @ManyToOne
    @JoinColumn(name = "repair_type")
    private DictInfo repairType;

    /**
     * 报修区域
     */
    @ManyToOne
    @JoinColumn(name = "area_id")
    private Area reportArea;

    /**
     * 负责人
     */
    @ManyToOne
    @JoinColumn(name = "charge_user_id")
    private SystemUser chargeUser;
    /**
     * 关联企业
     */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    /**
     * 关联设备
     */
    @ManyToOne
    @JoinColumn(name = "facility_id")
    private Facility facility;


    /**
     * 工单状态
     */
    @Enumerated(value = EnumType.STRING)
    @Column(name = "repair_order_status")
    private RepairOrderStatusEnum repairOrderStatusEnum;

    /**
     * 报修单类型
     */
    @Enumerated(value = EnumType.STRING)
    @Column(name = "repair_order_type")
    private RepairOrderTypeEnum repairOrderTypeEnum;


    /**
     * 报修照片
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "report_photo", joinColumns = {
            @JoinColumn(name = "report_order_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> reportPhoto = new HashSet<Resource>();

    /**
     * 到场照片
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "presence_photo", joinColumns = {
            @JoinColumn(name = "report_order_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> presencePhoto = new HashSet<>();

    /**
     * 完成照片
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "finish_photo", joinColumns = {
            @JoinColumn(name = "report_order_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> finishPhoto = new HashSet<>();

    /**
     * 录音
     */
    @ManyToOne
    @JoinColumn(name = "recording_id")
    private Resource resource;

    /**
     * 400报修录音路径
     */
    private String recordingPath;

    /**
     * 客户报修人
     */
    private String customer;

    /**
     * 400主叫号码
     */
    private String src;

    /**
     * 电话
     */
    private String telephone;

    /**
     * 手机
     */
    private String mobile;

    /**
     * 来电号码
     */
    private String callNumber;

    /**
     * 总工时
     */
    private long totalWorkingHours;

    /**
     * 我的工时
     */
    private long workingHours;


    /**
     * 完成审批流名称
     */
    private String flowName;

    /**
     * 当前完成审批到第几级
     */
    private int auditLevel;

    /**
     * 完成审批流级数
     */
    private int level;

    /**
     * 备品备件
     */
    private String spareParts;

    /**
     * 当前审批部门
     */
    @ManyToOne
    @JoinColumn(name = "audit_organization_id")
    private Organization auditOrganization;

    /**
     * 当前审批人
     */
    @ManyToOne
    @JoinColumn(name = "auidt_user_id")
    private SystemUser auditUser;

    /**
     * 审批岗位
     */
    @ManyToOne
    @JoinColumn(name = "audit_position_id")
    private Position auditPosition;


    /**
     * 到场后的经度
     */
    @Column(precision = 12, scale = 6)
    private BigDecimal longitude;

    /**
     * 到场后的纬度
     */
    @Column(precision = 12, scale = 6)
    private BigDecimal latitude;

    /**
     * 完成审批流（[{"organizationId":"1","nodeTypeEnum":"USER","relationId":"2"},{}]）
     */
    @Column(columnDefinition = "longtext")
    private String flow;

    /**
     * 故障分类
     */
    @ManyToOne
    @JoinColumn(name = "trouble_cat_id")
    private TroubleCat troubleCat;


    /**
     * 是否指派
     */
    @Column(columnDefinition = "bit default 0")
    private boolean assign;

    /**
     * app临时保存工时计算的标记
     */
    @Column(columnDefinition = "bit default 0")
    private boolean mark;

    /**
     * 指派人
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "assign_users", joinColumns = {
            @JoinColumn(name = "report_order_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")})
    private Set<SystemUser> assignUsers = new HashSet<>();

    /**
     * 指派岗位
     */
    @ManyToMany
    @JoinTable(name = "assign_positions", joinColumns = {
            @JoinColumn(name = "report_order_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "position_id", referencedColumnName = "id")})
    private Set<Position> assignPositions = new HashSet<>();

    /**
     * 指派类型
     */
    @Enumerated(value = EnumType.STRING)
    @Column(name = "assign_type")
    private AssignTypeEnum assignTypeEnum;

    /**
     * 审核通过的人
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "audited_users", joinColumns = {
            @JoinColumn(name = "report_order_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")})
    private Set<SystemUser> hasAuditedUser = new HashSet<SystemUser>();


    @OneToMany(mappedBy = "repairOrder")
    private Set<RepairPartner> repairPartners = new HashSet<>();

    @ManyToOne
    @JoinColumn(name = "auditFlow_id")
    private AuditFlow auditFlow;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date saveTriggerTime;

    /**
     * 终审备注
     */
    private String lastRemark;

    /**
     * 终审评分
     */
    @Column(columnDefinition = "int default 0")
    private int lastScore;
    /**
     * 设备位置
     */
    private String position;


    private boolean checkRepair=false;
    /**
     * 维修确认时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date confirmDate;
    /**
     * 签字时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date signDate;

    /**
     * 终审评分
     */

    public String getReportCode() {
        return reportCode;
    }

    public void setReportCode(String reportCode) {
        this.reportCode = reportCode;
    }

    public SystemUser getReportUser() {
        return reportUser;
    }

    public void setReportUser(SystemUser reportUser) {
        this.reportUser = reportUser;
    }

    public String getReportAddress() {
        return reportAddress;
    }

    public void setReportAddress(String reportAddress) {
        this.reportAddress = reportAddress;
    }

    public String getReportContent() {
        return reportContent;
    }

    public void setReportContent(String reportContent) {
        this.reportContent = reportContent;
    }

    public SystemUser getReceiveUser() {
        return receiveUser;
    }

    public void setReceiveUser(SystemUser receiveUser) {
        this.receiveUser = receiveUser;
    }

    public SystemUser getTelephoneUser() {
        return telephoneUser;
    }

    public void setTelephoneUser(SystemUser telephoneUser) {
        this.telephoneUser = telephoneUser;
    }

    public DictInfo getReportMajor() {
        return reportMajor;
    }

    public void setReportMajor(DictInfo reportMajor) {
        this.reportMajor = reportMajor;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public RepairSourceEnum getRepairSourceEnum() {
        return repairSourceEnum;
    }

    public void setRepairSourceEnum(RepairSourceEnum repairSourceEnum) {
        this.repairSourceEnum = repairSourceEnum;
    }

    public Date getRequirePresenceTime() {
        return requirePresenceTime;
    }

    public void setRequirePresenceTime(Date requirePresenceTime) {
        this.requirePresenceTime = requirePresenceTime;
    }

    public Date getRequireFinishTime() {
        return requireFinishTime;
    }

    public void setRequireFinishTime(Date requireFinishTime) {
        this.requireFinishTime = requireFinishTime;
    }

    public Date getRepairFinishTime() {
        return repairFinishTime;
    }

    public void setRepairFinishTime(Date repairFinishTime) {
        this.repairFinishTime = repairFinishTime;
    }

    public Boolean getInRoutingScope() {
        return inRoutingScope;
    }

    public void setInRoutingScope(Boolean inRoutingScope) {
        this.inRoutingScope = inRoutingScope;
    }

    public String getFaultReason() {
        return faultReason;
    }

    public void setFaultReason(String faultReason) {
        this.faultReason = faultReason;
    }

    public String getMeasure() {
        return measure;
    }

    public void setMeasure(String measure) {
        this.measure = measure;
    }

    public String getUnrepairReason() {
        return unrepairReason;
    }

    public void setUnrepairReason(String unrepairReason) {
        this.unrepairReason = unrepairReason;
    }

    public Boolean getInRepairScope() {
        return inRepairScope;
    }

    public void setInRepairScope(Boolean inRepairScope) {
        this.inRepairScope = inRepairScope;
    }


    public DictInfo getRepairType() {
        return repairType;
    }

    public void setRepairType(DictInfo repairType) {
        this.repairType = repairType;
    }

    public Area getReportArea() {
        return reportArea;
    }

    public void setReportArea(Area reportArea) {
        this.reportArea = reportArea;
    }

    public SystemUser getChargeUser() {
        return chargeUser;
    }

    public void setChargeUser(SystemUser chargeUser) {
        this.chargeUser = chargeUser;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public Facility getFacility() {
        return facility;
    }

    public void setFacility(Facility facility) {
        this.facility = facility;
    }

    public RepairOrderStatusEnum getRepairOrderStatusEnum() {
        return repairOrderStatusEnum;
    }

    public void setRepairOrderStatusEnum(RepairOrderStatusEnum repairOrderStatusEnum) {
        this.repairOrderStatusEnum = repairOrderStatusEnum;
    }

    public RepairOrderTypeEnum getRepairOrderTypeEnum() {
        return repairOrderTypeEnum;
    }

    public void setRepairOrderTypeEnum(RepairOrderTypeEnum repairOrderTypeEnum) {
        this.repairOrderTypeEnum = repairOrderTypeEnum;
    }

    public Set<Resource> getReportPhoto() {
        return reportPhoto;
    }

    public void setReportPhoto(Set<Resource> reportPhoto) {
        this.reportPhoto = reportPhoto;
    }

    public Set<Resource> getPresencePhoto() {
        return presencePhoto;
    }

    public void setPresencePhoto(Set<Resource> presencePhoto) {
        this.presencePhoto = presencePhoto;
    }

    public Set<Resource> getFinishPhoto() {
        return finishPhoto;
    }

    public void setFinishPhoto(Set<Resource> finishPhoto) {
        this.finishPhoto = finishPhoto;
    }

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getCallNumber() {
        return callNumber;
    }

    public void setCallNumber(String callNumber) {
        this.callNumber = callNumber;
    }

    public long getTotalWorkingHours() {
        return totalWorkingHours;
    }

    public void setTotalWorkingHours(long totalWorkingHours) {
        this.totalWorkingHours = totalWorkingHours;
    }

    public long getWorkingHours() {
        return workingHours;
    }

    public void setWorkingHours(long workingHours) {
        this.workingHours = workingHours;
    }


    public String getFlowName() {
        return flowName;
    }

    public void setFlowName(String flowName) {
        this.flowName = flowName;
    }

    public int getAuditLevel() {
        return auditLevel;
    }

    public void setAuditLevel(int auditLevel) {
        this.auditLevel = auditLevel;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public Organization getAuditOrganization() {
        return auditOrganization;
    }

    public void setAuditOrganization(Organization auditOrganization) {
        this.auditOrganization = auditOrganization;
    }

    public SystemUser getAuditUser() {
        return auditUser;
    }

    public void setAuditUser(SystemUser auditUser) {
        this.auditUser = auditUser;
    }

    public Position getAuditPosition() {
        return auditPosition;
    }

    public void setAuditPosition(Position auditPosition) {
        this.auditPosition = auditPosition;
    }

    public String getFlow() {
        return flow;
    }

    public void setFlow(String flow) {
        this.flow = flow;
    }

    public TroubleCat getTroubleCat() {
        return troubleCat;
    }

    public void setTroubleCat(TroubleCat troubleCat) {
        this.troubleCat = troubleCat;
    }

    public Boolean getAssign() {
        return assign;
    }

    public void setAssign(Boolean assign) {
        this.assign = assign;
    }

    public Set<SystemUser> getAssignUsers() {
        return assignUsers;
    }

    public void setAssignUsers(Set<SystemUser> assignUsers) {
        this.assignUsers = assignUsers;
    }

    public Set<Position> getAssignPositions() {
        return assignPositions;
    }

    public void setAssignPositions(Set<Position> assignPositions) {
        this.assignPositions = assignPositions;
    }

    public AssignTypeEnum getAssignTypeEnum() {
        return assignTypeEnum;
    }

    public void setAssignTypeEnum(AssignTypeEnum assignTypeEnum) {
        this.assignTypeEnum = assignTypeEnum;
    }

    public BigDecimal getLongitude() {
        return longitude;
    }

    public void setLongitude(BigDecimal longitude) {
        this.longitude = longitude;
    }

    public BigDecimal getLatitude() {
        return latitude;
    }

    public void setLatitude(BigDecimal latitude) {
        this.latitude = latitude;
    }

    public Set<SystemUser> getHasAuditedUser() {
        return hasAuditedUser;
    }

    public void setHasAuditedUser(Set<SystemUser> hasAuditedUser) {
        this.hasAuditedUser = hasAuditedUser;
    }

    public RepairLevel getReportLevel() {
        return reportLevel;
    }

    public void setReportLevel(RepairLevel reportLevel) {
        this.reportLevel = reportLevel;
    }

    public String getSpareParts() {
        return spareParts;
    }

    public void setSpareParts(String spareParts) {
        this.spareParts = spareParts;
    }

    public Set<RepairPartner> getRepairPartners() {
        return repairPartners;
    }

    public void setRepairPartners(Set<RepairPartner> repairPartners) {
        this.repairPartners = repairPartners;
    }

    public Date getPresenceTime() {
        return presenceTime;
    }

    public void setPresenceTime(Date presenceTime) {
        this.presenceTime = presenceTime;
    }

    public Boolean getTransfer() {
        return transfer;
    }

    public void setTransfer(Boolean transfer) {
        this.transfer = transfer;
    }

    public AuditFlow getAuditFlow() {
        return auditFlow;
    }

    public void setAuditFlow(AuditFlow auditFlow) {
        this.auditFlow = auditFlow;
    }

    public String getLastRemark() {
        return lastRemark;
    }

    public void setLastRemark(String lastRemark) {
        this.lastRemark = lastRemark;
    }

    public int getLastScore() {
        return lastScore;
    }

    public void setLastScore(int lastScore) {
        this.lastScore = lastScore;
    }

    public Boolean getMark() {
        return mark;
    }

    public void setMark(Boolean mark) {
        this.mark = mark;
    }

    public Date getReceiveDate() {
        return receiveDate;
    }

    public void setReceiveDate(Date receiveDate) {
        this.receiveDate = receiveDate;
    }

    public boolean isInRoutingScope() {
        return inRoutingScope;
    }

    public void setInRoutingScope(boolean inRoutingScope) {
        this.inRoutingScope = inRoutingScope;
    }

    public boolean isTransfer() {
        return transfer;
    }

    public void setTransfer(boolean transfer) {
        this.transfer = transfer;
    }

    public boolean isInRepairScope() {
        return inRepairScope;
    }

    public void setInRepairScope(boolean inRepairScope) {
        this.inRepairScope = inRepairScope;
    }

    public boolean isAssign() {
        return assign;
    }

    public void setAssign(boolean assign) {
        this.assign = assign;
    }

    public boolean isMark() {
        return mark;
    }

    public void setMark(boolean mark) {
        this.mark = mark;
    }

    public Date getSaveTriggerTime() {
        return saveTriggerTime;
    }

    public void setSaveTriggerTime(Date saveTriggerTime) {
        this.saveTriggerTime = saveTriggerTime;
    }

    public String getMajorName() {
        return majorName;
    }

    public void setMajorName(String majorName) {
        this.majorName = majorName;
    }


    public String getRecordingPath() {
        return recordingPath;
    }

    public void setRecordingPath(String recordingPath) {
        this.recordingPath = recordingPath;
    }

    public String getSrc() {
        return src;
    }

    public void setSrc(String src) {
        this.src = src;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public boolean isCheckRepair() {
        return checkRepair;
    }

    public void setCheckRepair(boolean checkRepair) {
        this.checkRepair = checkRepair;
    }

    public Date getConfirmDate() {
        return confirmDate;
    }

    public void setConfirmDate(Date confirmDate) {
        this.confirmDate = confirmDate;
    }

    public Date getSignDate() {
        return signDate;
    }

    public void setSignDate(Date signDate) {
        this.signDate = signDate;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("reportCode", StringConverters.null2String(getReportCode()));
        jsonObject.put("inRoutingScope", StringConverters.null2Boolean(getInRoutingScope()));
        jsonObject.put("inRoutingScopeName", StringConverters.null2Boolean(getInRoutingScope()) ? "是" : "否");
        jsonObject.put("assign",StringConverters.null2Boolean(getAssign()) ? "是" : "否");
        jsonObject.put("transfer", StringConverters.null2Boolean(getTransfer()));
        jsonObject.put("orderStatus", getRepairOrderStatusEnum().getName());
        jsonObject.put("orderStatusEnum", getRepairOrderStatusEnum().getCode());
        jsonObject.put("repairOrderTypeEnum",getRepairOrderTypeEnum().getCode());
        jsonObject.put("repairOrderType",getRepairOrderTypeEnum().getName());
        jsonObject.put("reportUserName", getReportUser() == null ? "" : getReportUser().getUserName());
        jsonObject.put("receiveUser", getReceiveUser() == null ? "" : getReceiveUser().getId());
        jsonObject.put("reportLevel", getReportLevel() == null ? "" : getReportLevel().getGrade());
        jsonObject.put("reportMajor", getReportMajor() == null ? "" : getReportMajor().getPropertyName());
        jsonObject.put("majorName",StringConverters.null2String(getMajorName()));
        jsonObject.put("reportLevelId", getReportLevel() == null ? "" : getReportLevel().getId());
        jsonObject.put("reportMajorId", getReportMajor() == null ? "" : getReportMajor().getId());
        jsonObject.put("source", getRepairSourceEnum().getName());
        jsonObject.put("reportUser", getReportUser() != null ? getReportUser().getUserName() : "");
        jsonObject.put("mobile", getMobile());
        jsonObject.put("callNumber",  StringConverters.null2String(getCallNumber()));
        jsonObject.put("checkRepair",StringConverters.null2Boolean(isCheckRepair()) ? "是" : "否");
        jsonObject.put("auditLevel",getAuditLevel());
        jsonObject.put("level",getLevel());
        jsonObject.put("src",getSrc());
        jsonObject.put("customer", StringConverters.null2String(getCustomer()));
        jsonObject.put("repairType", getRepairType() == null ? "" : getRepairType().getPropertyName());
        if (getConfirmDate() != null) {
            jsonObject.put("confirmDate", DateTimeUtils.format(getConfirmDate(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            jsonObject.put("confirmDate", "");
        }
        if (getSignDate() != null) {
            jsonObject.put("signDate", DateTimeUtils.format(getSignDate(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            jsonObject.put("signDate", "");
        }
        if (getRepairFinishTime() != null) {
            jsonObject.put("repairFinishTime", DateTimeUtils.format(getRepairFinishTime(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            jsonObject.put("repairFinishTime", "");
        }
        if (getReceiveDate() != null) {
            jsonObject.put("receiveDate", DateTimeUtils.format(getReceiveDate(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            jsonObject.put("receiveDate", "");
        }

        if (getRequireFinishTime() != null) {
            jsonObject.put("requireFinishTime", DateTimeUtils.format(getRequireFinishTime(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            jsonObject.put("requireFinishTime", "");
        }

        if (getPresenceTime() != null) {
            jsonObject.put("presenceTime", DateTimeUtils.format(getPresenceTime(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            jsonObject.put("presenceTime", "");
        }
        if (getRequirePresenceTime() != null) {
            jsonObject.put("requirePresenceTime", DateTimeUtils.format(getRequirePresenceTime(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            jsonObject.put("requirePresenceTime", "");
        }
        jsonObject.put("reportUserMobile", getReportUser() == null ? "" : getReportUser().getMobile());
        List<Resource> reportPhoto = new ArrayList<>(getReportPhoto());
        reportPhoto.sort((o1, o2) -> o1.getId() < o2.getId() ? -1 : 1);
        JSONArray reportPhotoArray = new JSONArray();
        if (CollectionUtils.isNotEmpty(reportPhoto)) {
            for (Resource resource : reportPhoto) {
                reportPhotoArray.add(resource.getPath());
            }
        }
//        JSONArray partnerArray = new JSONArray();
//        List<TaskPartner> repairPartners = new ArrayList<>(getRepairPartners());
//        repairPartners.sort((o1, o2) -> o1.getId() < o2.getId() ? -1 : 1);
//        if (CollectionUtils.isNotEmpty(repairPartners)) {
//            for (TaskPartner repairPartner : repairPartners) {
//                JSONObject partnerObject = new JSONObject();
//                partnerObject.put("userName", repairPartner.getPartner().getUserName());
//                partnerObject.put("userId", repairPartner.getPartner().getId());
//                partnerObject.put("partnerId",repairPartner.getId());
//                partnerObject.put("workingHours", repairPartner.getWorkingHours());
//                partnerArray.add(partnerObject);
//            }
//        }
        List<Resource> presencePhoto = new ArrayList<>(getPresencePhoto());
        presencePhoto.sort((o1, o2) -> o1.getId() < o2.getId() ? -1 : 1);
        JSONArray presencePhotoArray = new JSONArray();
        JSONArray presencePhotoIdArray = new JSONArray();
        if (CollectionUtils.isNotEmpty(presencePhoto)) {
            for (Resource resource : presencePhoto) {
                presencePhotoArray.add(resource.getPath());
                presencePhotoIdArray.add(resource.getId());
            }
        }
        List<Resource> finishPhoto = new ArrayList<>(getFinishPhoto());
        finishPhoto.sort((o1, o2) -> o1.getId() < o2.getId() ? -1 : 1);
        JSONArray finishPhotoArray = new JSONArray();
        JSONArray finishPhotoIdArray = new JSONArray();
        if (CollectionUtils.isNotEmpty(finishPhoto)) {
            for (Resource resource : finishPhoto) {
                finishPhotoArray.add(resource.getPath());
                finishPhotoIdArray.add(resource.getId());
            }
        }
        jsonObject.put("recording", getResource() == null ? "" : getResource().getPath());
        jsonObject.put("spareParts", StringConverters.null2String(getSpareParts()));
        jsonObject.put("unrepairReason", StringConverters.null2String(getUnrepairReason()));
        jsonObject.put("measure", StringConverters.null2String(getMeasure()));
        jsonObject.put("faultReason", StringConverters.null2String(getFaultReason()));
        jsonObject.put("workingHours", StringConverters.null2String(getWorkingHours()));
        jsonObject.put("totalWorkingHours", StringConverters.null2String(getTotalWorkingHours()));
        jsonObject.put("reportContent", StringConverters.null2String(getReportContent()));
        jsonObject.put("lastRemark", StringConverters.null2String(getLastRemark()));
        jsonObject.put("lastScore", StringConverters.null2Int(getLastScore()));
        jsonObject.put("reportPhoto", reportPhotoArray);
        jsonObject.put("reportPhoto1", reportPhotoArray!=null&&reportPhotoArray.size()>0? Configuration.getConfigurationByName("hostUrl")+reportPhotoArray.get(0).toString():"");
//        jsonObject.put("partner", partnerArray);
        jsonObject.put("presencePhoto", presencePhotoArray!=null?presencePhotoArray:"");
        jsonObject.put("presencePhoto1", presencePhotoArray!=null&&presencePhotoArray.size()>0?Configuration.getConfigurationByName("hostUrl")+presencePhotoArray.get(0).toString():"");

        jsonObject.put("presencePhotoIds", presencePhotoIdArray!=null?presencePhotoIdArray:"");
        jsonObject.put("finishPhotoIds",finishPhotoIdArray!=null? finishPhotoIdArray:"");
        jsonObject.put("finishPhoto", finishPhotoArray);
        jsonObject.put("finishPhoto1", finishPhotoArray!=null&&finishPhotoArray.size()>0?Configuration.getConfigurationByName("hostUrl")+finishPhotoArray.get(0).toString():"");

        //接单响应时间
        if(getReceiveDate()!=null&&getCreateDate()!=null){
            jsonObject.put("callBack", DateTimeUtils.getDistanceMinutes(getReceiveDate(),getCreateDate()));
        }else{
            jsonObject.put("callBack","");
        }
        //到场响应时间
        if(getReceiveDate()!=null&&getPresenceTime()!=null){
            jsonObject.put("callBackPresence", DateTimeUtils.getDistanceMinutes(getPresenceTime(),getReceiveDate()));
        }else{
            jsonObject.put("callBackPresence","");
        }
        jsonObject.put("facilityName", getFacility() == null ? "" : getFacility().getName());
        jsonObject.put("facilityId", getFacility() == null ? "" : getFacility().getId());
        jsonObject.put("facilityCode",getFacility() == null ? "" : getFacility().getCode());
        jsonObject.put("facilityCatName",getFacility() != null&&getFacility().getFacilityCat()!=null ? getFacility().getFacilityCat().getCatName():"");
        jsonObject.put("rootCatId",getFacility()!=null?getFacility().getRootCatId():"");
        jsonObject.put("reportArea", getReportArea() == null ? "" : getReportArea().getAreaName());
        jsonObject.put("reportAddress", StringConverters.null2String(getReportAddress()));
        jsonObject.put("workingHour", getWorkingHours());
        jsonObject.put("receiveUserName", getReceiveUser() == null ? "" : getReceiveUser().getUserName());
        jsonObject.put("receiveUser", getReceiveUser() == null ? "" : getReceiveUser().getId());
        jsonObject.put("enterpriseName", StringConverters.null2String(getEnterprise().getEnterpriseName()));
        jsonObject.put("enterpriseId", StringConverters.null2String(getEnterprise().getId()));
        jsonObject.put("inRepairScope", StringConverters.null2Boolean(getInRoutingScope()));
        jsonObject.put("inRepairScopeName", StringConverters.null2Boolean(getInRoutingScope()) ? "是" : "否");
        jsonObject.put("troubleCat", getTroubleCat() == null ? "" : getTroubleCat().getTroubleCatName());
        jsonObject.put("troubleCatId", getTroubleCat() == null ? "" : getTroubleCat().getId());
        jsonObject.put("organizationName", getOrganization() == null ? "" : getOrganization().getOrganizationName());
        jsonObject.put("mark", StringConverters.null2Boolean(getMark()));
        jsonObject.put("assignType",getAssignTypeEnum()!=null&&getAssign()?(assignTypeEnum.getCode().equals("USER")?"指派人员":"指派岗位"):"未指派");
        List<SystemUser>assignUsers = new ArrayList<>(getAssignUsers());
        assignUsers.sort((o1, o2) -> o1.getId() < o2.getId() ? -1 : 1);
        JSONArray usersArray = new JSONArray();
        for(SystemUser systemUser: assignUsers){
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("userName",StringConverters.null2String(systemUser.getUserName()));
            jsonObject1.put("userId",systemUser.getId());
            usersArray.add(jsonObject1);
        }

        List<Position>assignPositions = new ArrayList<>(getAssignPositions());
        assignPositions.sort((o1, o2) -> o1.getId() < o2.getId() ? -1 : 1);
        JSONArray postionsArray = new JSONArray();
        for(Position position : assignPositions){
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("positionName",StringConverters.null2String(position.getName()));
            jsonObject2.put("positionId",position.getId());
            postionsArray.add(jsonObject2);
        }

        jsonObject.put("position", StringConverters.null2String(getPosition()));
        jsonObject.put("facilityPosition", StringConverters.null2String(getPosition()));
        jsonObject.put("usersArray",usersArray);
        jsonObject.put("postionsArray",postionsArray);
        //语音
        if(!getRepairOrderTypeEnum().getCode().equals("_400_ORDER")){
            jsonObject.put("word",getResource()!=null?Configuration.getConfigurationByName("hostUrl")+getResource().getPath():"");

        }else{
            jsonObject.put("word",Configuration.getConfigurationByName("recordUrl")+getRecordingPath());
        }


        //报修来源
        jsonObject.put("repairSourceEnum",getRepairSourceEnum()!=null?getRepairSourceEnum().getName():"");
        //接线人
        jsonObject.put("telephoneUser",getTelephoneUser()!=null?getTelephoneUser().getUserName():"");
        //到场响应时间
        if(getPresenceTime()!=null&&getCreateDate()!=null){
            jsonObject.put("responseTime",TimeUtils.formatDateAgo1(DateTimeUtils.getDiffSecondsTime(getPresenceTime(),getCreateDate())));
        }else{
            jsonObject.put("responseTime","");

        }

        // 返回实体添加返回审批层数
        String flow = StringConverters.null2String(getFlow());
        int flowLength = 0;
        if (flow != null && !flow.equals("")) {
            flow=flow.replace("[{","").replace("}]","");
            flowLength = flow.split("}").length;
        }

        jsonObject.put("flowSize", flowLength);


        return jsonObject;
    }


    public JSONObject toDashboardJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("reportContent", StringConverters.null2String(getReportContent()));

        jsonObject.put("reportCode", StringConverters.null2String(getReportCode()));
        jsonObject.put("reportLevel", getReportLevel() == null ? "" : getReportLevel().getGrade());
        jsonObject.put("lastRemark", StringConverters.null2String(getLastRemark()));
        jsonObject.put("majorName",StringConverters.null2String(getMajorName()));
        jsonObject.put("reportArea", getReportArea() == null ? "" : getReportArea().getAreaName());
        jsonObject.put("orderStatus", getRepairOrderStatusEnum().getName());
        jsonObject.put("repairOrderType", getRepairOrderTypeEnum().getName());
        jsonObject.put("receiveUserName", getReceiveUser() == null ? "" : getReceiveUser().getUserName());
        return jsonObject;
    }

}

